package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Business;
import com.dingyh.shopwebbackground.entity.Conversation;
import com.dingyh.shopwebbackground.entity.User;
import com.dingyh.shopwebbackground.service.BusinessService;
import com.dingyh.shopwebbackground.service.ConversationService;
import com.dingyh.shopwebbackground.service.UserService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-03-07
 */
@RestController
@CrossOrigin
@RequestMapping("/conversation")
public class ConversationController {

    @Autowired
    ConversationService conversationService;

    @Autowired
    UserService userService;

    @Autowired
    BusinessService businessService;

    /**
     * 获取商家的聊天列表
     * @param saleId
     * @return
     */
    @RequestMapping("/getUserListID")
    public CommonResult<Object> getUserListID(String saleId){
        List<Conversation> conversationList = conversationService.getUserListID(saleId);
        conversationList.forEach(conversation -> {
            User user = userService.getById(conversation.getUserId());
            conversation.setNickName(user.getNickname());
            List<Conversation> conversations = conversationService.getNotRead(saleId,conversation.getUserId());
            conversation.setCount(conversations.size());
        });
        System.out.println(conversationList);
        return new CommonResult(200,"",conversationList);
    }

    /**
     * 买家 / 管理员获取聊天记录列表
     * @param userId
     * @return
     */
    @RequestMapping("/getSaleListID")
    public CommonResult<Object> getSaleListID(String userId){
        List<Conversation> conversationList = conversationService.getSaleListID(userId);
        conversationList.forEach(conversation -> {
            User user = userService.getById(conversation.getSaleId());
            Business business = businessService.selectByUserId(user.getId());
            conversation.setNickName(business.getSaleName());
        });
        return new CommonResult(200,"",conversationList);
    }

    /**
     * 获取聊天记录(商家页面)
     * @param saleId
     * @param userId
     * @return
     */
    @RequestMapping("/getTalkingByUserID")
    public CommonResult<Object> getTalkingByUserID(String saleId,String userId){
        List<Conversation> conversationList = conversationService.getTalkingByUserID(saleId, userId);
        conversationList.forEach(conversation -> {
            User user = userService.getById(conversation.getUserId());
            Business business = businessService.selectByUserId(conversation.getSaleId());
            conversation.setAvatarSale(business.getSaleLogo());
            conversation.setAddress(user.getTel());
            conversation.setNickName(user.getNickname());
            conversation.setAvatar(user.getAvatar());
            if (conversation.getShopTalking().equals("")){
                conversation.setIfRead("1");
            }
        });
        return new CommonResult<>(200,"",conversationList);
    }

    /**
     * 管理员 / 买家获取聊天记录
     * @param saleId
     * @param userId
     * @return
     */
    @RequestMapping("/getTalkingBySaleId")
    public CommonResult<Object> getTalkingBySaleId(String saleId,String userId){
        List<Conversation> conversationList = conversationService.getTalkingByUserID(saleId, userId);
        conversationList.forEach(conversation -> {
            User sale = userService.getById(conversation.getUserId());
            Business business = businessService.selectByUserId(conversation.getSaleId());
            User user = userService.getById(conversation.getUserId());
            conversation.setAddress(user.getTel());
            conversation.setNickName(business.getSaleName());
            conversation.setAvatar(business.getSaleLogo());
            if (conversation.getShopTalking().equals("")){
                conversation.setIfRead("1");
            }
        });
        return new CommonResult<>(200,"",conversationList);
    }


    @RequestMapping("/saleSend")
    public CommonResult<Object> saleSend(String saleId,String userId,String shopTalking){
        Conversation conversation = Conversation.builder()
                .id(UUIDUtil.generator())
                .saleId(saleId)
                .userId(userId)
                .shopTalking(shopTalking)
                .userTalking("")
                .timeSaid(System.currentTimeMillis())
                .ifRead("0")
                .build();
        boolean result = conversationService.save(conversation);
        if (result){
            return new CommonResult<>(200,"",null);
        }else {
            return new CommonResult(500, "发送失败！", null);
        }
    }

    @RequestMapping("/userSend")
    public CommonResult<Object> userSend(String saleId,String userId,String userTalking){
        Conversation conversation = Conversation.builder()
                .id(UUIDUtil.generator())
                .saleId(saleId)
                .userId(userId)
                .userTalking(userTalking)
                .shopTalking("")
                .ifRead("0")
                .timeSaid(System.currentTimeMillis())
                .build();
        boolean result = conversationService.save(conversation);
        if (result){
            return new CommonResult<>(200,"",null);
        }else {
            return new CommonResult(500, "发送失败！", null);
        }
    }
}

